from django.views.generic.list import ListView
from django.contrib.auth.models import User, Permission, PermissionManager
from django import forms
from django.http import HttpResponse

class MyUser(User):
    class Meta:
        proxy = True
#        model = User
        
    def has_markets_perm(self):
        print "Permissions for user %s on 'jrc_market_change'" % self.username
        return self.has_perm("jrc_market_change")

    def has_tradingsystems_perm(self):
        print "Permissions for user %s on 'jrc_tradingsystems_change'" % self.username
        return self.has_perm("jrc_tradingsystems_change")

    def has_pet_perm(self):
        print "Permissions for user %s on 'PET'" % self.username
        return self.has_perm("jrc_pet_change")

    def has_import_perm(self):
        print "Permissions for user %s on 'jrc_import_change'" % self.username
        return self.has_perm("jrc_import_change")

class UserListView(ListView):
    model = MyUser
    template_name = "user/user_list.html"
    
    def get_context_data(self, *args, **kwargs):
        context = super(ListView, self).get_context_data(*args, **kwargs)
        p = Permission.objects.get(codename = "jrc_pet_change")
        print "JRC PET permission: %s" % p
        context["pet_perm_pk"] = p.pk
        
        p = Permission.objects.get(codename = "jrc_market_change")
        print "JRC markets permission: %s" % p
        context["markets_perm_pk"] = p.pk
        
        p = Permission.objects.get(codename = "jrc_tradingsystems_change")
        print "JRC Trading-systems permission: %s" % p
        context["tradingsystems_perm_pk"] = p.pk
        
        p = Permission.objects.get(codename = "jrc_import_change")
        print "JRC Import permission: %s" % p
        context["import_perm_pk"] = p.pk
        
        return context


def dochangeperm(request, userpk, perm):
    p = Permission.objects.get(pk = perm)
    print "Permission to change: %s" % p
    user_to_change = User.objects.get(pk = userpk)
    print "User to apply permission is: %s" % user_to_change
    
    permission_name = "%s.%s" % (p.content_type.app_label, p.codename)
    
    print "Has user %s permission %s? %s" % (user_to_change.username, permission_name, user_to_change.has_perm(permission_name))
    has = ""

    if user_to_change.has_perm(permission_name):
        print "Removing permission %s from user %s" % (permission_name, user_to_change.username)
        user_to_change.user_permissions.remove(p)
    else:
        has = "checked='checked'"
        print "Addign permission %s to user %s" % (permission_name, user_to_change.username)
        user_to_change.user_permissions.add(p)

    # Saving changes to permissions
    user_to_change.save()
    print "Has user %s permission %s? %s" % (user_to_change.username, permission_name, user_to_change.has_perm(permission_name))

#    return HttpResponse("Ok", content_type = "text/html")
    return HttpResponse("<input type=\"checkbox\" %s onchange=\"OnJrcPermissionChanged(%s, %s);\"/>" % (has, userpk, perm), content_type = "text/html")
